package com.yex.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yex.common.exception.RRException;
import com.yex.dao.ArticleDao;
import com.yex.dao.NoticeDao;
import com.yex.entity.ArticleEntity;
import com.yex.entity.NoticeEntity;
import com.yex.utils.Query;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ArticleService extends ServiceImpl<NoticeDao, NoticeEntity> {

    @Value("${upload.host_name}")
    private String host_name;

    public List<NoticeEntity> listPage(Map<String, Object> params) {
        Page<NoticeEntity> page = new Query<NoticeEntity>(params).getPage();
        return baseMapper.selectPage(page, new EntityWrapper<>());
    }

    public List<NoticeEntity> list() {
        EntityWrapper<NoticeEntity> wrapper = new EntityWrapper<>();
        wrapper.eq("status", 1);
        List<NoticeEntity> entities = baseMapper.selectList(wrapper);
        if (entities == null || entities.size() == 0) return null;
        for (NoticeEntity item : entities) {
            item.setUrl(host_name + "/appApi/views/article/detail.html?id=" + item.getId());
        }
        return entities;
    }

    public NoticeEntity detail(Integer id) {
        NoticeEntity articleEntity = baseMapper.selectById(id);
        if (articleEntity == null || articleEntity.getStatus().compareTo(1) != 0) throw new RRException("公告不存在或已被删除");
        return articleEntity;
    }

//    public void add(ArticleEntity articleEntity) {
//        Integer count = baseMapper.insert(articleEntity);
//        if (count == null || count < 0) throw new RRException("新增失败");
//    }
//
//    public void update(ArticleEntity articleEntity) {
//        Integer count = baseMapper.deleteById(articleEntity);
//        if (count == null || count < 0) throw new RRException("修改失败");
//    }
//
//    public void delete(Integer[] ids) {
//        for (Integer id : ids) {
//            ArticleEntity articleEntity = new ArticleEntity();
//            articleEntity.setId(id);
//            Integer count = baseMapper.deleteById(articleEntity);
//            if (count == null || count < 0) throw new RRException("删除失败");
//        }
//    }

}
